
-- MapBuffView 地图buff界面

local Buff = import(".Buff")

local ClassRef = app.mvc.createPanelClass(...)

local countIcon = 6 -- buff最多个数

function ClassRef.create(node)
    local sprite = ClassRef.extend(node)
    sprite.root = node
    sprite:init()
    return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)
	self:setVisible(true)

	self.buffs = {}
	for i = 1, countIcon do
		self.buffs[i] = Buff.create( self.root:getChildByName("buff_"..i))
		self.buffs[i]:setVisible(false)
	end
end

function ClassRef:startRefresh()
	-- TODO 不应该刷着么频繁，需要优化
	scheduler.newCron(handler(self,self.refreshBuffs), 1, true)
end

function ClassRef:refreshBuffs()
	if cc.isDead(self) then
		return
	end
	if DungeonManager:isRace() then
		self:setVisible(false)
		return
	else
		self:setVisible(true)
	end
	local buffsToshow = {}
	-- 插入疲劳度buff
	table.insert( buffsToshow, { uid = 0, tid = 0, remainTime = 0, destroyTime = 0 } )

	local buffIds = UD:getSortMapBuffIds()

	for _,v in ipairs( buffIds ) do
		local buff = UD:getMapBuffByUid(v)
		if buff then
			local curTime = math.floor(app.session:getServerTime() or app:getTime())
			if buff.destroyTime - curTime > 0 then
				buff.remainTime = buff.destroyTime - curTime
				table.insert( buffsToshow, buff )
			end
		end
	end

	--再插入领地系统的城镇
	local bc = app:getInst("BiddingController")

	if bc.battleMapModel and bc.challengeList and #bc.challengeList.infos > 0 then
		local towns = bc.challengeList.infos
		for _, town in ipairs(towns) do
			local t = bc.battleMapModel.hTownInfos[town.townId]
			if t then
				t.isTownIcon = true
				table.insert( buffsToshow, 2,t )
			end
		end
	end

	--加入经验buff
	local playerLv = UD:getLevel() --人当前等级
	local worldLockExpId = app.session:getWorldExpLockId() --当前世界经验锁等级
	local worldLockConfig = GD:getWorldExpLockConf()
	local curLockConfig = GD:getWorldExpLockConfById(worldLockExpId)
	--local serverOpenDays = app.session:getOpenServerDays()

	if playerLv < tonumber(curLockConfig.level) then
		local isSecondBuffAdd = 0 --判断是否属于第二个阶段的增益
		local usingLockInfo = nil
		for k,v in ipairs(worldLockConfig) do

			if tonumber(worldLockExpId) <= tonumber(v.ID) then
				break
			end
			
			if playerLv < tonumber(v.level) then
				isSecondBuffAdd = isSecondBuffAdd+1

				if not usingLockInfo then
					usingLockInfo = v
				end
			end


		end

		if usingLockInfo then
			local addExpPercend = 0
			if isSecondBuffAdd > 2 then
				addExpPercend = tonumber(usingLockInfo.expBuff2)
			else
				addExpPercend = tonumber(usingLockInfo.expBuff1)
			end

			if addExpPercend > 0 and  isSecondBuffAdd >= 2 then
			table.insert( buffsToshow, 2, { uid = 0, tid = 1, remainTime = 0, destroyTime = 0, lockInfo = usingLockInfo, isSecondExpAdd = (isSecondBuffAdd>2) } )
			end
		end
	end

	-- 插入挑战赛icon
	local challengeNotify = app:getInst("PvpModel"):getChallengePrepareNotify()
	if challengeNotify then
		table.insert(buffsToshow, 2, {tid = 2})
	end

	for i = 1, countIcon do
		local data = buffsToshow[i]
		if data then
			self.buffs[i]:setVisible( true )
			self.buffs[i]:setData( data )
		else
			self.buffs[i]:setVisible( false )
		end
	end
end

return ClassRef

